/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.infosystem.noeplaza.lms.struts.action;

import com.eit.ntc.spring.utils.DateTimeUtils;
import com.eit.ntc.spring.utils.RandomUtils;
import com.eit.toschoolonline.lms.constant.LMSConstant;
import com.eit.toschoolonline.lms.frontend.jdo.LearningItemHistoryM;
import com.eit.toschoolonline.lms.frontend.jdo.LearningItemViewM;
import com.eit.toschoolonline.lms.frontend.service.LearningItemHistoryService;
import com.eit.toschoolonline.lms.frontend.service.LmsViewService;
//import com.eit.toschoolonline.lms.jdo.LearningItemM;
//import com.eit.toschoolonline.lms.service.LearningItemHistoryService;
//import com.eit.toschoolonline.lms.service.LearningItemService;
//import com.eit.toschoolonline.lms.service.LmsServiceFactory;
import com.eit.toschoolonline.openid.jdo.OpenIdUserM;
import com.eit.toschoolonline.openid.jdo.OpenIdUserRoleM;
import com.eit.toschoolonline.openid.service.spring.OpenIdService;
import java.util.Calendar;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionRedirect;

/**
 *
 * @author Administrator
 */
public class OpenIdShowItemAction extends Action{
     private LmsViewService lmsService;
     private OpenIdService openIdService;
     private LearningItemHistoryService historyService;


     private OpenIdUserM selfRegister(String claim) throws Exception{
        Calendar calendar = Calendar.getInstance(Locale.ENGLISH);
        OpenIdUserM claimUser = new OpenIdUserM();
        claimUser.setUsername(claim);
        claimUser.setPassword(RandomUtils.randomString(20));
        claimUser.setEnabled(1);
        claimUser.setLastLoginTime(calendar.getTime());


        OpenIdUserRoleM role = openIdService.getOpenIdUserRole("ROLE_OPENID_USER");

        claimUser.getAuthorities().add(role);
        claimUser = openIdService.createOpenIdUser(claimUser);
        return claimUser;
     }
     public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,HttpServletResponse response) throws Exception {
        String id = request.getParameter("id");
        String entType = request.getParameter("entType");
//        LearningItemService itemService = getLmsServiceFactory().getLearningItemService();
       



        
        


        
        String username = request.getRemoteUser();
        OpenIdUserM openIdUser = (OpenIdUserM) request.getSession().getAttribute("OpenId");
        System.out.println("Authenticated with OpenId-System:"+username);
        if (openIdUser==null){

             if (username!=null && !username.trim().equals("")){ // authenticated
                System.out.println("Login User:"+username);
                 openIdUser = openIdService.getOpenIdUser(username);
                if (openIdUser==null){ //
                    // authenticated but not register into application
                    openIdUser = selfRegister(username);
                    System.out.println("Authenticated -- Self Register ");
                };

                request.getSession().setAttribute("OpenId", openIdUser);
                System.out.println("Set OpenId-User Information into session");
            }

         };
//        System.out.print("user-history:"+username);
        String refId = RandomUtils.randomString(30,RandomUtils.baseString);
        if (id!=null && !id.trim().equals("")){
            int idInt = Integer.parseInt(id);
            LearningItemViewM item = lmsService.getItemViewByItemId(idInt);
            if (item==null){
                return mapping.findForward("notFoundItem");
            };
            LearningItemHistoryM itemHistory = new LearningItemHistoryM();
            itemHistory.setAccessingDateTime(DateTimeUtils.getEngNow());
            itemHistory.setFinishDateTime(DateTimeUtils.getEngNow());
            itemHistory.setLearningItem(item.getItemId());
            itemHistory.setLearningCode(item.getItemCode());
            itemHistory.setUsername(username);
            itemHistory.setSessionId(request.getSession().getId());
            itemHistory.setRefId(refId);
            if (item.getItemType()==LearningItemViewM.ECOURSE_TYPE){
                itemHistory.setEcourseFlag(LearningItemHistoryM.ITEM_FLAG);
            };
            if (item.getItemType()==LearningItemViewM.ELAB_TYPE){
                itemHistory.setElabFlag(LearningItemHistoryM.ITEM_FLAG);
            };
            if (item.getItemType()==LearningItemViewM.ETEST_TYPE){
                itemHistory.setEtestFlag(LearningItemHistoryM.ITEM_FLAG);
            };
            if (item.getItemType()==LearningItemViewM.EDOCUMENT_TYPE){
                itemHistory.setEdocFlag(LearningItemHistoryM.ITEM_FLAG);
            };
             if (item.getItemType()==LearningItemViewM.E_ENTRANS_TYPE){
                itemHistory.setEentFlag(LearningItemHistoryM.ITEM_FLAG);
            };
//            LearningItemHistoryService historyService = getLmsServiceFactory().getLearningItemHistoryService();
            itemHistory = historyService.createLearningItemHistory(itemHistory);
            String targetAction ="";
            switch(item.getItemType()){
                case LMSConstant.ItemTypeConstant.ITEM_COURSE_TYPE:
                case LMSConstant.ItemTypeConstant.ITEM_LAB_TYPE:
                case LMSConstant.ItemTypeConstant.ITEM_TEST_TYPE:
                case LMSConstant.ItemTypeConstant.IMAGE_JPEG_TYPE:
                case LMSConstant.ItemTypeConstant.PLAIN_TEXT_TYPE:
                case LMSConstant.ItemTypeConstant.FLASH_TYPE:
                    targetAction="/eitem.do";
                    break;
                case LMSConstant.ItemTypeConstant.HTML_DOC_TYPE:
                    targetAction="/eitem.do";
                    //targetAction="/forwardItem.do";
                    break;
                 case LMSConstant.ItemTypeConstant.VIDEO_MP3_TYPE:
                 case LMSConstant.ItemTypeConstant.FLASH_VIDEO_TYPE:
                    targetAction="/eitem.do";
                    //targetAction="/forwardItem.do";
                    break;
                case LMSConstant.ItemTypeConstant.ITEM_DOCUMENT_TYPE:
                    targetAction="/captchaAction.do?method=setUrl2Form&url=/downloadItem.do?path="+item.getItemCode()+"%26refId="+refId+"%26id="+item.getItemId();
//                    ActionRedirect ar = new ActionRedirect(targetAction);
//                    return ar;
                case LMSConstant.ItemTypeConstant.ITEM_ENTRANCE_TYPE:
                    if (entType == null || entType.equals("")){
                        targetAction="/eitem.do";
                    } else if (entType.equals("entOther")){
                        targetAction="/captchaAction.do?method=setUrl2Form&url=/downloadItem.do?path="+item.getItemCode()+"%26refId="+refId+"%26id="+item.getItemId();
//                        ActionRedirect arEnt = new ActionRedirect(targetAction);
//                        return arEnt;
                    }
                    break;
                case LMSConstant.ItemTypeConstant.PDF_TYPE:
                case LMSConstant.ItemTypeConstant.MS_POWERPOINT_TYPE:
//                targetAction="/downloadItem.do";
                targetAction="/captchaAction.do?method=setUrl2Form&url=/downloadItem2.do?refId="+refId;
                //+"%26id="+item.getItemId();
//                ActionRedirect ar = new ActionRedirect(targetAction);
//                return ar;
                    break;


            }
//            if (item.getItemType()==LearningItemViewM.ECOURSE_TYPE){
//                targetAction="/eitem.do";
//            };
//            if (item.getItemType()==LearningItemViewM.ELAB_TYPE){
//                targetAction="/eitem.do";
//            };
//            if (item.getItemType()==LearningItemViewM.ETEST_TYPE){
//                targetAction="/eitem.do";
//            };
//            if (item.getItemType()==LearningItemViewM.EDOCUMENT_TYPE){
//
//                targetAction="/captchaAction.do?method=setUrl2Form&url=/downloadItem.do?path="+item.getItemCode()+"%26refId="+refId+"%26id="+item.getItemId();
//                ActionRedirect ar = new ActionRedirect(targetAction);
//                return ar;
//            };
//            if (item.getItemType()==LearningItemViewM.E_ENTRANS_TYPE){
//                if (entType == null || entType.equals("")){
//                    targetAction="/eitem.do";
//                } else if (entType.equals("entOther")){
//                    targetAction="/captchaAction.do?method=setUrl2Form&url=/downloadItem.do?path="+item.getItemCode()+"%26refId="+refId+"%26id="+item.getItemId();
//                    ActionRedirect ar = new ActionRedirect(targetAction);
//                    return ar;
//                }
//            };
//            if (item.getItemType()==LMSConstant.ItemTypeConstant.IMAGE_JPEG_TYPE){
////                targetAction="/downloadItem.do";
//                targetAction="/eitem.do";
//            };
//            if (item.getItemType()==LMSConstant.ItemTypeConstant.PDF_TYPE){
////                targetAction="/downloadItem.do";
//                targetAction="/captchaAction.do?method=setUrl2Form&url=/downloadItem2.do?refId="+refId;
//                //+"%26id="+item.getItemId();
//                ActionRedirect ar = new ActionRedirect(targetAction);
//                return ar;
//            };
            if (!targetAction.equals("")){
                ActionRedirect ar = new ActionRedirect(targetAction);
                //ar.addParameter("path", new Integer(item.getItemPath()));
                ar.addParameter("refId", refId);
                //ar.addParameter("id", new Integer(item.getItemId()));
                return ar;
            };

        };
       return mapping.findForward("notFoundItem");
     }

//    public LmsServiceFactory getLmsServiceFactory() {
//        return lmsServiceFactory;
//    }
//
//    public void setLmsServiceFactory(LmsServiceFactory lmsServiceFactory) {
//        this.lmsServiceFactory = lmsServiceFactory;
//    }

    public LearningItemHistoryService getHistoryService() {
        return historyService;
    }

    public void setHistoryService(LearningItemHistoryService historyService) {
        this.historyService = historyService;
    }

    public LmsViewService getLmsService() {
        return lmsService;
    }

    public void setLmsService(LmsViewService lmsService) {
        this.lmsService = lmsService;
    }

    /**
     * @return the openIdService
     */
    public OpenIdService getOpenIdService() {
        return openIdService;
    }

    /**
     * @param openIdService the openIdService to set
     */
    public void setOpenIdService(OpenIdService openIdService) {
        this.openIdService = openIdService;
    }
}
